﻿using System.Collections;
using System.IO;
using System.Net;
using KingTools.CommonsHelper.Model;
using KingTools.Utils;

namespace KingTools.SocketHelper.Model
{
    /// <summary>
    ///     业务协议封装对象
    /// </summary>
    public class ProtocolBusinessVO
    {
        public ProtocolBusinessVO()
        {
        }

        public ProtocolBusinessVO(ArrayList data)
        {
            this.data = data;
        }

        public ProtocolBaseVO baseVO { get; set; }

        public ArrayList data { get; set; }

        public void GetBytes(MemoryStream stream)
        {
            if (data.Count > 0)
            {
                var writer = new BinaryWriter(stream);
                foreach (Item item in data)
                {
                    switch (item.name)
                    {
                        case "byte":
                            writer.Write(byte.Parse(item.value));
                            break;
                        case "int":
                            writer.Write(IPAddress.HostToNetworkOrder(int.Parse(item.value)));
                            break;
                        case "short":
                            writer.Write(IPAddress.HostToNetworkOrder(short.Parse(item.value)));
                            break;
                        case "string":
                            ByteBuffer.putString(stream, item.value);
                            break;
                        case "long":
                            writer.Write(IPAddress.HostToNetworkOrder(long.Parse(item.value)));
                            break;
                    }
                }
            }
        }
    }
}